package x.com;

import java.util.Set;
import java.util.TreeSet;

class Animal implements Comparable<Animal>
{
    private int age;
    private String name;

    public Animal(int a, String n)
    {
        this.age = a;
        this.name = n;
    }

    // Getter methods
    public int getAge()
    {
        return this.age;
    }

    public String getName()
    {
        return this.name;
    }

    // Setter methods
    public void setAge(int a)
    {
        this.age = a;
    }

    public void setName(String n)
    {
        this.name = n;
    }

    // override toString() method of Object class
    public String toString()
    {
        return "name is " + this.name + " and age is :" + this.age;
    }

    @Override
    public int compareTo(Animal obj)
    {
        // TODO Auto-generated method stub
        //int res = this.getName().compareTo(obj.getName());
        int res = this.getAge() - obj.getAge();

        if (res == 0)
        {
            return this.getName().compareTo(obj.getName());
        }
        else
        {
            return res;
        }

    }

}

public class testAnimal
{
    public static void main(String args[])
    {
        Animal a3 = new Animal(11, "C");
        Animal a4 = new Animal(13, "D");
        Animal a1 = new Animal(10, "H");
        Animal a2 = new Animal(13, "C");
        Animal a5 = new Animal(21, "D");
        Animal a6 = new Animal(11, "H");
        Animal a7 = new Animal(10, "D");

        Set<Animal> aniVec = new TreeSet<Animal>();

        aniVec.add(a1);
        aniVec.add(a2);
        aniVec.add(a3);
        aniVec.add(a4);
        aniVec.add(a5);
        aniVec.add(a6);
        aniVec.add(a7);
        // System.out.println(aniVec);

        for(Animal a : aniVec)
        {
            System.out.println("name: " + a.getName() + " and age: " + a.getAge());
        }

    }

}
